import type { App } from 'vue'
import {
  createMemoryHistory,
  createRouter,
  createWebHashHistory,
} from 'vue-router/auto'
import { setupLayouts } from 'virtual:generated-layouts'

const { VITE_BASE_URL } = import.meta.env

function setupRouterGuard() {
  const router = createRouter({
    history: import.meta.env.SSR ? createMemoryHistory(VITE_BASE_URL) : createWebHashHistory(VITE_BASE_URL),
    base: '', // 添加这一行
    extendRoutes: (routes) => {
      return setupLayouts(routes)
    },
  })
  return router
}

// 安装vue路由
export async function install(app: App) {
  // 添加路由守卫
  const router = setupRouterGuard()
  app.use(router)
  await router.isReady() // https://router.vuejs.org/zh/api/index.html#isready
}
